package com.xunji.user.util;


import com.xunji.user.dto.MenuDto;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class MenuUtil {

    public static List<MenuDto> makePidTree(List<MenuDto> MenuDtos, Integer pid) {
        List<MenuDto> children = MenuDtos.stream().filter(x -> x.getPId().equals(pid)).sorted(Comparator.comparing(MenuDto::getWeight)).collect(Collectors.toList());
        List<MenuDto> successor = MenuDtos.stream().filter(x -> !x.getPId().equals(pid)).collect(Collectors.toList());
        children.forEach(x -> {
            x.setChildren(makePidTree(successor, x.getId()));
        });
        return children;
    }
}
